package app.remote.web.action;

import app.remote.entity.RssUrl;
import app.remote.service.RssService;
import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Enclosure;
import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.feed.synd.SyndFeed;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: haibo.wang
 * Date: 13-7-9
 * Time: 下午3:35
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class RssController {

    @Resource
    private RssService rssService;

    @RequestMapping("/home")
    public String homePage(Map<String, Object> cache) {
        List<RssUrl> urls = rssService.getHello();
        cache.put("urls", urls);
        System.out.println(urls);
        return "/WEB-INF/page/home.jsp";
    }

    @RequestMapping("/url/add")
    public String addUrl() {
        return "redirect:/home";
    }

    @RequestMapping("/url/setup")
    public String urlSetup(RssUrl rssUrl) {
        rssService.addUrl(rssUrl);
        return "redirect:/home";
    }

    @RequestMapping("/url/view/{id}")
    public String urlView(@PathVariable int id, Map<String, Object> cache) {
        Channel channel = rssService.getRssInfo(id);
        List<Item> items = channel.getItems();
        cache.put("items", items);
        return "/WEB-INF/page/rss_info.jsp";
    }

    @RequestMapping("/url/delete/{id}")
    public String deleteUrl(@PathVariable int id) {
        rssService.deleteUrl(id);
        return "redirect:/home";
    }

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/rest/rss")
    @ResponseBody
    public Channel rssTest() {
        Channel channel = restTemplate.getForObject("http://chdbits.org/torrentrss.php?rows=10&linktype=dl&passkey=c6539a7521ea76b8dc31b905f0203301", Channel.class);
        return channel;
    }
}
